package com.flong.modules.service;

import java.sql.SQLException;
import java.util.List;

import com.flong.modules.AppException;
import com.flong.modules.BaseService;
import com.flong.modules.model.oracle.UserColComments;
import com.flong.modules.model.oracle.UserConsColumns;
import com.flong.modules.utils.DBHelper;
import com.flong.modules.utils.ObjectUtil;

@SuppressWarnings("all")
public class UserConsColumnsService  extends BaseService {
	private static String USER_CONS_COLUMNS_FILED_SQL = " ";
	/**
	 * @Author		liangjilong
	 * @Date		2017年2月23日 下午2:30:56
	 * @throws Exception 		参数
	 * @return 		List<String> 返回类型
	 */
	public static List<UserConsColumns> getUserConsColumns() throws Exception {
		conn = DBHelper.getInstance().getConnection(); //链接数据库
		if(ObjectUtil.isNotEmpty(dialect) && "ORACLE".equalsIgnoreCase(dialect)){
			sql = "select owner, constraint_name, table_name, column_name, position from  user_cons_columns";
		}else if(ObjectUtil.isNotEmpty(dialect) && "SQLSERVER".equalsIgnoreCase(dialect)){
		}else if(ObjectUtil.isNotEmpty(dialect) && "MYSQL".equalsIgnoreCase(dialect)){
		}else{
			throw new AppException(1,"方言配置有误，目前只支持MYSQL和Oracle");
		}
		return retColList(sql);
	}
	
	
	/**
	 * @Description TODO
	 * @Author		liangjilong
	 * @Date		2017年3月2日 下午3:03:47
	 * @param tableName
	 * @return
	 * @throws Exception 		参数
	 * @return 		UserConsColumns 返回类型
	 */
	public static UserConsColumns getUserConsColumnsByName(String tableName) throws Exception {
		conn = DBHelper.getInstance().getConnection(); //链接数据库
		if(ObjectUtil.isNotEmpty(dialect) && "ORACLE".equalsIgnoreCase(dialect)){
			sql = " select A.* from user_cons_columns A, user_constraints B  where A.constraint_name = B.constraint_name and B.constraint_type = 'P' AND upper(A.table_name) =upper('"+tableName+"')";
		}else if(ObjectUtil.isNotEmpty(dialect) && "SQLSERVER".equalsIgnoreCase(dialect)){
		}else if(ObjectUtil.isNotEmpty(dialect) && "MYSQL".equalsIgnoreCase(dialect)){
		}else{
			throw new AppException(1,"方言配置有误，目前只支持MYSQL和Oracle");
		}
		List<UserConsColumns> retColList = retColList(sql);
		if(ObjectUtil.isNotEmpty(retColList)){
			return retColList(sql).get(0);
		}
		return null;
	}
	/**
	 * @Description TODO
	 * @Author		liangjilong
	 * @Date		2017年3月2日 下午1:49:48
	 * @param sql
	 * @return
	 * @throws SQLException
	 * @throws Exception 		参数
	 * @return 		List<UserConsColumns> 返回类型
	 */
	private static List<UserConsColumns> retColList(String sql) throws SQLException, Exception {
		psmt = conn.prepareStatement(sql);
		rs = psmt.executeQuery();
		List<UserConsColumns> convertToEntitys = DBHelper.convertToEntitys(rs, UserConsColumns.class);
		DBHelper.releaseAll();
		return convertToEntitys;
	}
	
}
